package com.fbd.apipassenger.service;

import com.fbd.apipassenger.remote.ServicePassengerUserClient;
import com.fbd.apipassenger.remote.ServiceVerificationCodeClient;
import com.fbd.internalcommon.constant.IdentityConstant;
import com.fbd.internalcommon.constant.TokenConstants;
import com.fbd.internalcommon.dto.ResponseResult;
import com.fbd.internalcommon.dto.VerificationCodeDTO;
import com.fbd.internalcommon.response.NumberCodeResponse;
import com.fbd.internalcommon.response.TokenResponse;
import com.fbd.internalcommon.util.JwtUtils;
import com.fbd.internalcommon.util.RedisPrefixUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

import static com.fbd.internalcommon.constant.CommonStatusEnum.VERIFICATION_CODE_ERROR;
@Service
public class VerificationCodeService {

    @Autowired
    private ServiceVerificationCodeClient serviceVerificationCodeClient;
    @Autowired
    private ServicePassengerUserClient servicePassengerUserClient;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    public ResponseResult generatorCode(String passengerPhone){
        // 获取验证码
        ResponseResult<NumberCodeResponse> numberCodeResponseResponseResult = serviceVerificationCodeClient.numberCode(6);
        int code = numberCodeResponseResponseResult.getData().getNumberCode();
        String key = RedisPrefixUtils.generatorKeyPhone(passengerPhone);
        //存入redis
        stringRedisTemplate.opsForValue().set(key , code+"" , 2 , TimeUnit.MINUTES);
        return ResponseResult.success("");
    }

    public ResponseResult checkCode(VerificationCodeDTO verificationCodeDTO){
        // 根据手机号获取验证码
        String passengerPhone = verificationCodeDTO.getPassengerPhone();
        System.out.println("根据手机号，去reids中获取验证码");
        String key = RedisPrefixUtils.generatorKeyPhone(passengerPhone);
        String verificationCode = stringRedisTemplate.opsForValue().get(key);
        System.out.println("redis中的验证码为：" + verificationCode);
        // 校验验证码
        System.out.println("校验验证码");
        if (StringUtils.isBlank(verificationCode)){
            // 验证码不存在
            return ResponseResult.fail(VERIFICATION_CODE_ERROR.getCode(),VERIFICATION_CODE_ERROR.getValue());
        }
        if (!verificationCode.trim().equals(verificationCodeDTO.getVerificationCode().trim())){
            // 验证码不存在
            return ResponseResult.fail(VERIFICATION_CODE_ERROR.getCode(),VERIFICATION_CODE_ERROR.getValue());
        }
        // 判断是否有此用户
        ResponseResult responseResult = servicePassengerUserClient.loginOrRegister(verificationCodeDTO);
        System.out.println(responseResult.getMessage());
        // 颁发token令牌
        System.out.println("颁发token令牌");
        String refreshToken = JwtUtils.generatorToken(passengerPhone, IdentityConstant.PASSENGER_IDENTITY , TokenConstants.REFRESH_TOKEN_KEY);
        String accessToken = JwtUtils.generatorToken(passengerPhone, IdentityConstant.PASSENGER_IDENTITY , TokenConstants.ACCESS_TOKEN_KEY);
        // 将token放入redis中
        String refreshTokenKey = RedisPrefixUtils.generatorKeyToken(passengerPhone , TokenConstants.REFRESH_TOKEN_KEY);
        String accessTokenKey = RedisPrefixUtils.generatorKeyToken(passengerPhone , TokenConstants.ACCESS_TOKEN_KEY);
        stringRedisTemplate.opsForValue().set(refreshTokenKey , refreshToken , 30 , TimeUnit.DAYS);
        stringRedisTemplate.opsForValue().set(accessTokenKey , accessToken , 31 , TimeUnit.DAYS);
        TokenResponse tokenResponse = new TokenResponse();
        tokenResponse.setToken(refreshToken);
        return ResponseResult.success(tokenResponse);
    }
}
